//
//  ZWHLoginViewController.swift
//  茶吧
//
//  Created by Syrena on 2017/7/7.
//  Copyright © 2017年 Syrena. All rights reserved.
//

import UIKit
import Hyphenate
import AVOSCloud
import MBProgressHUD

protocol ZWHLoginViewControllerDelegate {
    func showselectIndex(page:Int)
}
class ZWHLoginViewController: UIViewController {
    @IBOutlet weak var loginB: UIButton!
    
    @IBOutlet weak var phoneF: UITextField!
    
    @IBOutlet weak var passwordF: UITextField!
    
    var page=0
    
    var delegate:ZWHLoginViewControllerDelegate?
    
    override func viewWillAppear(_ animated: Bool) {
        navigationController?.isNavigationBarHidden = false
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        setnavigation()
        loginB.isEnabled = false
        phoneF.delegate = self
        passwordF.delegate = self
    }

    //设置导航栏
    func setnavigation(){
        
        navigationItem.title = "登录"
        navigationController?.navigationBar.barTintColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        //定义标题颜色与字体大小字典
        let dict:NSDictionary = [NSForegroundColorAttributeName: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1),NSFontAttributeName : UIFont.boldSystemFont(ofSize: 13)]
        //标题设置颜色与字体大小
        
        navigationController?.navigationBar.titleTextAttributes = dict as? [String : AnyObject]
        
        let Btn = UIButton(frame: CGRect(x: 0, y: 0, width: 15, height: 15))
        Btn.setImage(UIImage.init(named:"黑色返回"), for: .normal)
        Btn.addTarget(self, action: #selector(backClicked), for: .touchUpInside)
        let back = UIBarButtonItem(customView: Btn)
        navigationItem.leftBarButtonItem = back
        let register = UIBarButtonItem(title: "注册", style: .plain, target: self, action: #selector(registerC))
        register.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.black,NSFontAttributeName:UIFont.boldSystemFont(ofSize: 13)], for: .normal)
        
        navigationItem.rightBarButtonItem = register
    }
    
    func registerC(){
                    let vc = UIStoryboard(name: "ZWHRegister", bundle: nil).instantiateViewController(withIdentifier: "ZWHRegisterViewController")
        
                    navigationController?.pushViewController(vc, animated: true)
    }
    
    func backClicked() {
        self.dismiss(animated: true, completion: nil)
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        view.endEditing(true)
    }
    
    //登录
    @IBAction func LoginBtnClicked(_ sender: UIButton) {
        MBProgressHUD.showAdded(to: view, animated: true)
        AVUser.logInWithUsername(inBackground: phoneF.text!, password: passwordF.text!) { [weak self](user, error) in
            if user != nil{
                EMClient.shared().login(withUsername: self?.phoneF.text!, password: self?.passwordF.text, completion: { [weak self](info, error) in
                    if error == nil{
                        EMClient.shared().options.isAutoLogin = true
                        //设置存储信息
                        UserDefaults.standard.set(true, forKey: "islogin")
                        UserDefaults.standard.synchronize()
                        MBProgressHUD.hideAllHUDs(for: self?.view, animated: true)
                        self?.delegate?.showselectIndex(page: (self?.page)!)
                        self?.dismiss(animated: true, completion: nil)
                    }else{
                        print(error ?? "")
                        MBProgressHUD.hideAllHUDs(for: self?.view, animated: true)
                        ZWHAlertController.showAlert(vc: self!, title: "账号密码错误", message: nil, style: .alert)
                    }
                })
            }else{
                MBProgressHUD.hideAllHUDs(for: self?.view, animated: true)
                ZWHAlertController.showAlert(vc: self!, title: "账号密码错误", message: nil, style: .alert)
            }
        }
    }
    
    //隐藏键盘
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        view.endEditing(true)
    }
    
}
extension ZWHLoginViewController:UITextFieldDelegate{
    func textFieldDidEndEditing(_ textField: UITextField, reason: UITextFieldDidEndEditingReason) {
        if phoneF.text != "" && passwordF.text != "" {
            loginB.isEnabled = true
            loginB.setTitleColor(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 1), for: .normal)
        }else{
            loginB.isEnabled = false
            loginB.setTitleColor(#colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1), for: .normal)
        }
    }
}
